<template>
    <div :class="className" :style="{height:'350px',width:'1400px'}" />
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

/* import View from '../iot/view/designer/view' */

export default {
/*  components: { View }, */
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '1400px'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    deviceCode: {
      type: String
    },


  },
  data() {
    return {
      chart: null,
      deviceDataName: [],
     	deviceDataTime: [],
      temperatureData: [],
      queryParams: {
        timeSlot: "1",
        startTime: undefined,
        endTime: undefined,
      },
      startEndTime: [],
      timeSlot: [],
      // 设备数据表格数据
      dataMap: undefined,
    }

  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if(val !== null && val !==undefined){
          for(var i=0;i<val.time.length;i++){
            val.time[i] = this.formatDate(val.time[i]);
          }
          this.setOptions(val)
        }
      }
    }
  },
  beforeMount() {
    /* this.getDicts('time_slot').then(response => {
      this.timeSlot = response.data;
    }); */
    //this.getList() ;
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    formatDate(date) {
      var date = new Date(date);
      var YY = date.getFullYear() + '-';
      var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
      var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
      var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
      var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
      return YY + MM + DD +" "+hh + mm + ss;
    },
    // getList() {
    //   this.loading = true;
    //   /* this.reset(); */
    //   this.queryParams.deviceCode=this.deviceCode
    //   selectTemperatureData(this.queryParams).then(response => {
    //     debugger;
    //     console.log(response)
    //       this.deviceDataTime = response.data.time;
    //       this.temperatureData = response.data.data;
    //       console.log(this.deviceDataTime)
    //       console.log(this.temperatureData)
    //       this.initChart()
    //   })},
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions(val) {
      this.chart.setOption(
        {
          title: {
            text: '温度折线图'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['A点红外温度', 'B点红外温度', 'C点红外温度', 'D点红外温度', 'E点红外温度', 'F点红外温度', 'G点红外温度']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          toolbox: {
            feature: {
              saveAsImage: {}
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: val.time
          },
          yAxis: {
            type: 'value'
          },
          series: val.data
        }
      )
    }
  }
}
</script>
